--[[
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
--]]

-- EXPERIMENTAL - WORK IN PROGRESS

-- LoadCode loads code files from one or more urls, and
-- then calls a success function or an error function.

function LoadCode(...)
  
  local successFun = arg[arg.n - 1]
  local errorFun = arg[arg.n]
  local numberOfActiveDownloads = arg.n - 2
  local success = true -- We are optimistic!
  
  local function handleDownloadFun(data)
    if nil ~= data then
      assert(loadstring(data))()
    else
      success = false
    end
    numberOfActiveDownloads = numberOfActiveDownloads - 1
    if 0 == numberOfActiveDownloads then
      if success then
        successFun()
      else
        errorFun()
      end
    end
  end
  
  for i,value in ipairs(arg) do
    if i < arg.n - 1 then
      LoadTextFile(value, handleDownloadFun)
    end
  end

end

function OnKeyDown(keyCode)
  if 49 == keyCode then -- MAK_1 49
    RunScript()
  elseif 50 == keyCode then -- MAK_2 50
    RunSelection()
  end
end

function RunScript()
  local url = "http://comikit.se/mobilelua/scripts/" .. ScriptAppCode .. ".lua"
  --local url = "http://localhost:4042/mobilelua/scripts/" .. ScriptAppCode .. ".lua"
  LoadCode(
    url,
    function() 
      Log('Success loading: ' .. url)
      if nil ~= OnInit then OnInit() end
      if nil ~= OnDraw then OnDraw() end 
    end,
    function() 
      Log('Failed loading: ' .. url) 
    end)
end

function RunSelection()
  local url = "http://comikit.se/mobilelua/scripts/" .. ScriptAppCode .. ".doit.lua"
  --local url = "http://localhost:4042/mobilelua/scripts/" .. ScriptAppCode .. ".doit.lua"
  LoadCode(
    url,
    function() 
      Log('Success loading: ' .. url) 
    end,
    function() 
      Log('Failed loading: ' .. url) 
    end)
end

ScriptAppCode = 'Undefined'

function SetScriptAppCode(appCode)
  ScriptAppCode = appCode
end

